package com.jsgt.greekfire.ui.main.activation.fragment

import android.app.Dialog
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseFragment
import com.fhx.core.utils.DialogUtil
import com.jsgt.greekfire.R
import com.jsgt.greekfire.common.util.PayPassWordUtil
import com.jsgt.greekfire.factory.WalletViewModelFactory
import com.jsgt.greekfire.viewmodel.WalletViewModel1
import kotlinx.android.synthetic.main.fragment_activated.*

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class ActivatedFragment : BaseFragment(), View.OnClickListener {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    private val walletViewModel1: WalletViewModel1 by lazy {
        ViewModelProvider(
            this,
            WalletViewModelFactory()
        ).get(WalletViewModel1::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_activated
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            ActivatedFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }

    override fun main() {

        walletViewModel1.activationTransferData.observe(viewLifecycleOwner, Observer {
            showProgress(false)
            showToast(getString(R.string.cloud_activation_transfer_success))
        })
        walletViewModel1.errorLiveData.observe(viewLifecycleOwner, Observer {
            showToast(it.toString())
            showProgress(false)
        })

        txvOk.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.txvOk -> {
                when {
                    edtAddress.text.isNullOrEmpty() -> {
                        showToast(getString(R.string.cloud_activation_address_hint))
                    }
                    edtAmount.text.isNullOrEmpty() -> {
                        showToast(getString(R.string.cloud_activation_amount_hint))
                    }
                    else -> {
//                        showInputPwdDialog()
                        PayPassWordUtil.show(context!!,"MET", edtAmount.text.toString()) {
                            showProgress(true)
                            //服务端验证
                            walletViewModel1.activationTransfer(
                                edtAddress.text.toString(),
                                edtAmount.text.toString(),
                                it
                            )
                        }
                    }
                }
            }
        }
    }

    /**
     * 输入密码弹窗
     */
    private var pwdDialog: Dialog? = null
    private fun showInputPwdDialog() {
        val contentView = layoutInflater.inflate(R.layout.dialog_input_pay_pwd, null, false)
        pwdDialog = DialogUtil.show(context!!, contentView)
        pwdDialog?.setOnDismissListener {
            pwdDialog = null
        }
        pwdDialog?.setCanceledOnTouchOutside(true)
        pwdDialog?.setCancelable(true)
        pwdDialog?.show()

        contentView.findViewById<TextView>(R.id.check_pwd_tv_cancel).setOnClickListener {
            pwdDialog?.dismiss()
        }

        contentView.findViewById<TextView>(R.id.check_pwd_tv).setOnClickListener {
            val pwd = contentView.findViewById<EditText>(R.id.pwd_et).text.toString().trim()
            if (TextUtils.isEmpty(pwd)) {
                showToast(getString(R.string.input_pwd_error_null))
            } else {
                //服务端验证
                walletViewModel1.activationTransfer(
                    edtAddress.text.toString(),
                    edtAmount.text.toString(),
                    pwd
                )
                pwdDialog?.dismiss()
                showProgress(true)
            }
        }
    }
}